<?php
/*
Plugin Name: Shiftt Notify (بىلوگىر ئەسكەرتكۈچى)
Plugin URI: https://cn.wordpress.org/plugins/Shiftt-notify/
Description: ۋوردپىرىس ئىنكاسلىرىنى ئۈندىدار سالونىدا ئەسكەرتىش
Version: 1.0
Author: ShiftBloger
Author URI: https://www.shiftt.cn/
*/

add_action( 'admin_menu', 'shiftt_baxkurux_tizimliki' );

function shiftt_baxkurux_tizimliki() {
       add_submenu_page(
              'tools.php',
              'ئەسكەرتكۈچى تەڭشىكى',
              'ئەسكەرتكۈچى',
              'manage_options', 
              'shiftt-setting',
              'shiftt_notify',
              1000 ); 
}

function shiftt_notify() { ?>  
    <h1>بىلوگىر ئەسكەرتكۈچى تەڭشىكى</h1>
    <br/>
    <p>ئەڭ ئاۋال سىزدە دەلىللەنگەن ئۈندىدار سالونى بولىشى كىرەك .</p>
    <p>سالون ئارقا سۇپىسى تەڭشىكى ئىچىدىن AppID ۋە APPsecret ئۇچۇرىنى ئىلىپ چىقىڭ . </p>
    <p>مەزكۇر مۇلازىمىتىرڭىز IP ئادىرىسىنى سالون ئاق تىزىملىكىگە كىرگۈزۈڭ .</p>   
    <br/>
        
    <form method="post" name="shiftt_set" id="shiftt_set">   
        
        <table class="form-table" role="presentation">
            <tbody>
                <tr>
                    <th scope="row"><label for="shiftt_appid"> AppID </label></th>
                    <td>
                        <input name="shiftt_appid" type="text" id="shiftt_appid" class="regular-text"
                        value="<?php echo get_option('shiftt_appid') ? esc_attr(get_option('shiftt_appid')) : ''; ?>" >
                    </td>
                </tr>
                <tr>
                    <th scope="row"><label for="shiftt_appsecret"> APPsecret </label></th>
                    <td>
                        <input name="shiftt_appsecret" type="text" id="shiftt_appsecret" class="regular-text" 
                        value="<?php echo get_option('shiftt_appsecret') ? esc_attr(get_option('shiftt_appsecret')) : ''; ?>"  >
                    </td>
                </tr>
                <tr>
                    <th scope="row"><label for="shiftt_wxtemplateid"> موبەن ئۇچۇر ID </label></th>
                    <td>
                        <input name="shiftt_wxtemplateid" type="text" id="shiftt_wxtemplateid" class="regular-text" 
                        value="<?php echo get_option('shiftt_wxtemplateid') ? esc_attr(get_option('shiftt_wxtemplateid')) : ''; ?>"  >
                    </td>
                </tr>
                <tr>
                    <th scope="row"><label for="shiftt_author_openid"> ئاپتور openID </label></th>
                    <td>
                        <input name="shiftt_author_openid" type="text" id="shiftt_author_openid" class="regular-text" 
                        value="<?php echo get_option('shiftt_author_openid') ? esc_attr(get_option('shiftt_author_openid')) : ''; ?>"  >
                    </td>
                </tr>
            </tbody>
        </table>
        <p class="submit">   
            <input type="submit" name="option_submit" id="option_submit" class="button button-primary" value="ساقلاش">
        </p> 
    </form>   
  
<?php  }   

if(isset($_POST['option_submit'])){   

    update_option('shiftt_appid',$_POST['shiftt_appid']);
    update_option('shiftt_appsecret',$_POST['shiftt_appsecret']);
    update_option('shiftt_wxtemplateid',$_POST['shiftt_wxtemplateid']);
    update_option('shiftt_author_openid',$_POST['shiftt_author_openid']); 
   
}  

add_action('wp_insert_comment', 'shiftt_send_msg');

function shiftt_send_msg($comment_id) {
    
    $appid = get_option('shiftt_appid');
    $appsecret = get_option('shiftt_appsecret');
    $wxtemplateid = get_option('shiftt_wxtemplateid');
    $author_openid = get_option('shiftt_author_openid');
    
    $comment = get_comment($comment_id);
    $post_id = $comment->comment_post_ID;
    $post   = get_post($post_id);
    $post_title = $post->post_title;
    
    $username = get_comment_author($comment_id) ;
    $datetime = $comment->comment_date ;
    $content = $comment->comment_content ;
    $link = get_comment_link($comment_id);
    

    $aurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;

    $body = wp_remote_retrieve_body( wp_remote_get( $aurl ) );
    $body = json_decode( $body, true );
    $access_token = $body['access_token'];

    $openid = $author_openid;
    $templateid = $wxtemplateid;
    $first = 'يازما : [' . $post_title . ']  غا يىڭى ئىنكاس يوللاندى ';
    
    $data1 = $username;
    $data2 = $datetime;
    $data3 = $content;
    $remark = "بۇيەرنى بىسىپ كىرىپ جاۋاب قايتۇرۇڭ";

    $wurl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;

    $data = '{
       "touser":"' . $openid . '",
       "template_id":"' . $templateid . '",
       "url":"'. $link .'", 
       "miniprogram":{
         "appid":"",
         "pagepath":""
       },         
       "data":{
               "first": {
                   "value":"' . $first . '"
               },
               "keyword1":{
                   "value":"' . $data1 . '"
               },
               "keyword2": {
                   "value":"' . $data2 . '"
               },
               "keyword3": {
                   "value":"' . $data3 . '"
               },
               "remark":{
                   "value":"' . $remark . '"
               }
            }
        }';
    $send = wp_remote_post($wurl, array(
        'body' => $data,
        'method' => 'POST',
        'headers' => array('Content-Type' => 'application/json'),
    ));   
    return $send;
    exit;
}

?>
